/**
 * @author Pro.Linyl
 * @CreateTime 2015年6月5日
 * @UpdateAuthor Pro.Linyl
 * @UpdateTime 2015年6月5日
 */
package http;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import java.util.List;

import util.BaseTools;
import util.Constrants;

/***
 * @author Pro.Linyl
 * @CreateTime 2015年6月5日
 * @UpdateAuthor Pro.Linyl
 * @UpdateTime 2015年6月5日
 * @description http请求操作类
 */
public class HttpThread {
    private static final String TAG = "HttpThread";
    /***
     * 上下文对象
     */
    private Context context;

    public HttpThread(Context context) {
        this.context = context;
    }


    /***
     * @param bodyBase http请求消息类(AJAX格式)
     * @author Pro.Linyl
     * @CreateTime 2015年6月5日
     * @UpdateAuthor Pro.Linyl
     */
    public void doRequest(final HttpBodyBase bodyBase) {
        if (bodyBase == null) {
            Log.e(Constrants.Http_Tag, "the HttpBodyBase is null");
            return;
        }
        if (!BaseTools.isConnection(context)) {
           Toast.makeText(bodyBase.getmContext(),"网络开小差了，请检查网络后重试",Toast.LENGTH_SHORT).show();
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    new HttpHelper().httpPost(context,bodyBase.getUrl(),bodyBase.params.getMaps(),getCallBack(bodyBase.mHandler, bodyBase.getAction()));
                }
            }).start();

        }
    }
    public void doRequest(final HttpBodyBase bodyBase, final List<String> imgs){
        if (bodyBase == null) {
            Log.e(Constrants.Http_Tag, "the HttpBodyBase is null");
            return;
        }
        if (!BaseTools.isConnection(context)) {
            Toast.makeText(bodyBase.getmContext(),"网络开小差了，请检查网络后重试",Toast.LENGTH_SHORT).show();
        }  else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    new HttpHelper().postImage(context,bodyBase.getUrl(),bodyBase.params.getMaps(),getCallBack(bodyBase.mHandler, bodyBase.getAction()),imgs);

                }
            }).start();
        }
    }
    /***
     * @param bodyBase http请求消息类(json格式)
     * @author Pro.Linyl
     * @CreateTime 2015年6月5日
     * @UpdateAuthor Pro.Linyl
     */
    /*public void doJsonRequest(final HttpBodyBase bodyBase) {
        if (bodyBase == null) {
            Log.e(FinalValue.Http_Tag, "the HttpBodyBase is null");
            return;
        }
        KJHttp kjh = new KJHttp();
        if (!CMBLTools.isConnection(context)) {
            ViewInject.toast(context.getString(R.string.err_net_link));
        } else {
            kjh.jsonPost(bodyBase.getUrl(), bodyBase.params, getCallBack(bodyBase.mHandler, bodyBase.getAction()));
        }
    }*/

    /***
     * 获取http操作监听
     *
     * @param handler 请求成功后返回监听
     * @param action  http请求action
     * @return HttpCallBack http操作监听
     */
    private HttpCallBack getCallBack(final Handler handler, final String action) {
        HttpCallBack httpCallBack = new HttpCallBack() {
            @Override
            public void onPreStart() {
                super.onPreStart();
            }

            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                Log.d(TAG, "onSuccess: " + response);
                Message msg = handler.obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putString("data", response);
                bundle.putString("action", action);
                msg.setData(bundle);
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                Log.d(TAG, "onFailure: " + strMsg);
            }

            @Override
            public void onLoading(long count, long current) {
                super.onLoading(count, current);
            }
        };
        return httpCallBack;
    }
}
